package org.bluedash.tryssl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class SSLClient {

	public static void main(String[] args) throws Exception {
		// Set the key store to use for validating the server cert.
		System.setProperty("javax.net.ssl.trustStore", Constant.CLIENT_KEY_STORE);

////////方法1/////////////////////////
//注意,这样的写法虽然可以,但是要注意一点就是BufferedReader的优点,它存在一个缓冲的概念,以至于保证信息传输的完整
//		SSLClient client = new SSLClient();
//		Socket s = client.clientWithoutCert();
//		OutputStream os = s.getOutputStream();
//		os.write("aef3sadfs".getBytes());
//		os.flush();
//		os.close();
//		s.close();
////////方法2 ////////////////////////
		
//		System.setProperty("javax.net.debug", "ssl,handshake");
		SSLClient client = new SSLClient();
		Socket s = client.clientWithoutCert();
		PrintWriter writer = new PrintWriter(s.getOutputStream());
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				s.getInputStream()));
		//向服务端发送hello
		writer.println("hello");
		writer.flush();
		System.out.println(reader.readLine());
		s.close();
		
		
	}

	private Socket clientWithoutCert() throws Exception {
		SocketFactory sf = SSLSocketFactory.getDefault();
		Socket s = sf.createSocket("localhost", Constant.PORT);
		return s;
	}
}